package xyz.zdsoft.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.boot.system.ApplicationHome;
import xyz.zdsoft.common.utils.RandomUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class FileUtils {
    public final static String[] fileExtensions = new String[]{"jpg","png","doc","docx","xls","xlsx","pdf","txt","rar","zip"};
	/**
	 * 导出Excel
	 * @param sheetName sheet名称
	 * @param title 标题
	 * @param values 内容
	 * @param wb HSSFWorkbook对象
	 */
	public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){

        // 第一步，创建一个HSSFWorkbook，对应一个Excel文件
        if(wb == null){
            wb = new HSSFWorkbook();
        }

        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();

        //声明列对象
        HSSFCell cell = null;

        //创建标题
        for(int i=0;i<title.length;i++){
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        //创建内容
        for(int i=0;i<values.length;i++){
            row = sheet.createRow(i + 1);
            for(int j=0;j<values[i].length;j++){
                //将内容按顺序赋给对应的列对象
                row.createCell(j).setCellValue(values[i][j]);
            }
        }
        return wb;
    }
    
    //发送响应流方法
    public static void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                //设置表文件名的字符编码，不然中文文件名会乱码
                fileName = new String(fileName.getBytes("utf-8"),"ISO-8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static boolean isFileExtension(String fileExtension) {
	    if(fileExtension == null || fileExtension.equals(""))
	        return false;
	    for(String fE:fileExtensions){
	        if(fE.equals(fileExtension))
	            return true;
        }
	    return false;
    }
    public static String getFileName(){
	    String fileName =  new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	    return  fileName+ RandomUtils.getSixBitRandom();
    }
    public static String mkdirs(ApplicationHome h){
        File jarF = h.getSource();
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String dirPath = jarF.getParentFile().toString() + "/upload/";
        System.out.println(dirPath);
        File filePath = new File(dirPath);
        //文件目录是否存在
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        return dirPath;
    }

    public static String mkdirscheckcode(ApplicationHome h){
        File jarF = h.getSource();
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String dirPath = jarF.getParentFile().toString() + "/upload/img/checkcode/";
        System.out.println("this dirpath:"+dirPath);
        File filePath = new File(dirPath);
        //文件目录是否存在
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        return dirPath;
    }

    public static String mkdirsLogo(ApplicationHome h){
        File jarF = h.getSource();
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String dirPath = jarF.getParentFile().toString() + "/upload/img/logo/";
        System.out.println("this dirpath:"+dirPath);
        File filePath = new File(dirPath);
        //文件目录是否存在
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        return dirPath;
    }

/*    public static String getPath(ApplicationHome h,String path){
        File jarF = h.getSource();
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String dirPath = jarF.getParentFile().toString() + "\\upload\\img\\" + path + "\\";
        File filePath = new File(dirPath);
        //文件目录是否存在
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        return dirPath;
    }*/

    public static String getLogoPath(ApplicationHome h){
        File jarF = h.getSource();
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String dirPath = jarF.getParentFile().toString() + "\\upload\\img\\logo\\";
        File filePath = new File(dirPath);
        //文件目录是否存在
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        return dirPath;
    }

    public static String getCheckCodePath(ApplicationHome h){
        File jarF = h.getSource();
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String dirPath = jarF.getParentFile().toString() + "/upload/img/checkcode/";
        File filePath = new File(dirPath);
        //文件目录是否存在
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        return dirPath;
    }
}
